﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using static System.Net.Mime.MediaTypeNames;

namespace ExportTool
{
    public class ExcelExport
    {
        public void ExportExcelLocal<T>(IEnumerable<T> list, string outpath, Action<int, CallbBackOBJ> callback, string fileName = "")
        {
            fileName = string.Format("{0}_{1:yyyyMMddHHmmssfff}.xlsx", (fileName == "") ? Guid.NewGuid().ToString() : fileName, DateTime.Now);
            string text = outpath + fileName;
            if (!Directory.Exists(outpath))
            {
                Directory.CreateDirectory(outpath);
            }

            try
            {
                string path = Path.Combine(Environment.CurrentDirectory, text);
                ExcelHelp.ExportToExcel(list, path);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            finally {
                FileInfo fileInfo = new FileInfo(text);
                long length = fileInfo.Length;
                callback.Invoke(1, new CallbBackOBJ {
                    Text = text,
                    FileName = fileName,
                    Length = length
                });
            }
            
        }
    }
    public  class CallbBackOBJ
    {
        public  string Text { get; set; } = "";
        public  string FileName { get; set; } = "";
        public  long Length { get; set; } = long.MinValue;
    }
}
